﻿using UnityEngine;
using System.Collections;

public class RightWdws : MonoBehaviour {
	public int WdwNum = 0; // 1 - Hire/ 2 - UNIT / 3 - BUILD / 4 - STAT
	public GameObject RightBtn;
	public GameObject RightWdwMgr;

	bool UpdateOnce = true;
	bool IN_POS_BeforeSwipe;

	void OnEnable(){
		IN_POS_BeforeSwipe = true;
		UpdateOnce = true;

		gameObject.GetComponent<TweenPosition> ().Play(true);
		transform.parent.GetComponent<UIPanel> ().depth = 1;
		RightWdwMgr.GetComponent<RightWdwMgr>().CurWinNum = WdwNum;

        GameObject.Find("MainCamera").GetComponent<AudioSource>().PlayOneShot(Resources.Load<AudioClip>("Audio/Drawer_Open"));

        if (GameObject.Find("BuildUI").transform.childCount != 0)
        {
            for (int i = 0; i < GameObject.Find("BuildUI").transform.childCount; ++i)
            {
                Destroy(GameObject.Find("BuildUI").transform.GetChild(i).gameObject);
                Destroy(GameObject.Find("Price(Clone)").gameObject);
            }
        }
	}

	public void OnClick(){
        if (!transform.parent.parent.GetComponent<RightWdwMgr>().m_bSelectUnitPage)
        {
            RightWdwMgr.GetComponent<RightWdwMgr>().CurWinNum = 0;
            //GameObject.Find("Top").GetComponent<TopUI>().UIPopDown(false);
            GameObject.Find("MainCamera").GetComponent<AudioSource>().PlayOneShot(Resources.Load<AudioClip>("Audio/Drawer_Close"));
        }
        else
        {
            GameObject WarnMsg = Instantiate(Resources.Load("Prefaps/UI/StaticUI/Warn/WarnMessageLabel") as GameObject) as GameObject;
            WarnMsg.transform.parent = transform.parent;
            WarnMsg.GetComponent<UILabel>().text = GameObject.Find("SelectUnitExplain").GetComponent<UILabel>().text;
        }
	}

	public void DelWdw(){
		gameObject.SetActive (false);
        RightBtn.SetActive(true);
	}

	public void InPos(){
		if(IN_POS_BeforeSwipe){
			gameObject.GetComponent<TweenPosition> ().from = new Vector3 ( -89.0f, 0.0f );
			gameObject.GetComponent<TweenPosition> ().to = Vector3.zero;
		}
	}

	public void SwipeToRightWdw(){
		transform.parent.GetComponent<UIPanel> ().depth = 0;

		if (gameObject.transform.localPosition.x == -89.0f) {
			gameObject.GetComponent<TweenPosition> ().enabled = true;
			gameObject.GetComponent<TweenPosition> ().from = gameObject.transform.localPosition;
			gameObject.GetComponent<TweenPosition> ().to = Vector3.zero;
			gameObject.GetComponent<TweenPosition> ().ResetToBeginning ();
			IN_POS_BeforeSwipe = true;

		} else if (gameObject.transform.localPosition.x != -89.0f) { // 다안도착햇을때
			gameObject.GetComponent<TweenPosition> ().Play(false);
			IN_POS_BeforeSwipe = false;
		}

		gameObject.GetComponent<TweenPosition> ().RemoveOnFinished(new EventDelegate(DelWdw));
		EventDelegate.Add (gameObject.GetComponent<TweenPosition> ().onFinished, DelWdw);
	}

	void OnDisable(){

		gameObject.GetComponent<TweenPosition> ().enabled = true;

		if (IN_POS_BeforeSwipe) {
			gameObject.GetComponent<TweenPosition> ().ResetToBeginning();

			gameObject.GetComponent<TweenPosition> ().from = Vector3.zero;
			gameObject.GetComponent<TweenPosition> ().to = new Vector3 ( -89.0f, 0.0f );

		} else {
		}
		
		gameObject.GetComponent<TweenPosition> ().RemoveOnFinished(new EventDelegate(DelWdw));
		gameObject.transform.localPosition = Vector3.zero;
		
		RightBtn.GetComponent<RightButtons> ().WdwClose ();
	}

	void Update(){
		if ( UpdateOnce && RightWdwMgr.GetComponent<RightWdwMgr> ().CurWinNum != WdwNum) {
			SwipeToRightWdw();
			UpdateOnce = false;
		}

        if (Input.GetKeyUp(KeyCode.Escape))
        {
            if (!(GameObject.Find("NickNameSetPaper") != null
                || GameObject.Find("UnitNoteDetails(Clone)") != null))
                OnClick();
        }
	}
}
